public class ReverseWords {
    public  static  String reverseWords(String s) {
        if (s.equals(""))
            return "";
        String[] str = s.split(" ");
        if (str.length==0)
            return "";
        else {
            String ans = "";
            for (int i=0;i<str.length-1;i++)
            {
                if (str[str.length-1-i].equals(""))
                    continue;
                else {
                    ans += str[str.length-1-i]+" ";
                }
            }
            if (str[0].equals(""))
                return ans.substring(0,ans.length()-1);
            else {
                return ans+str[0];
            }


        }


    }

    public static void main(String[] args) {
        String s= "  hello world!  ";
        System.out.println(reverseWords(s));
    }
}
